/**
 * @Author: Bobby
 * @Date: 2021-12-30 10:01:42
 * @LastEditTime: 2022-01-08 19:19:28
 * @LastEditors: Bobby
 * @Description:
 * @FilePath: \wms-v2\src\common\project\store\index.js
 * @CIMC-V2
 */
import { createStore } from 'vuex'
import createPersistedState from 'vuex-persistedstate'

const modules = {}
// 导入所有 vuex 模块，自动加入namespaced:true，用于解决vuex命名冲突，请勿修改
const commonFiles = require.context('./modules', false, /\.js$/)
commonFiles.keys().forEach((key) => {
  modules[key.replace(/(modules|\/|\.|js)/g, '')] = {
    ...commonFiles(key).default,
    namespaced: true,
  }
})
// project下 vuex模块
const projectFiles = require.context('@project/store', false, /\.js$/)
projectFiles.keys().forEach((key) => {
  modules[key.replace(/(modules|\/|\.|js)/g, '')] = {
    ...projectFiles(key).default,
    namespaced: true,
  }
})
const store = createStore({
  modules,
  plugins: [
    createPersistedState({
      paths: [''], //填写需要持久化的module名称
      key: `CIMC-IV-${process.env.VUE_APP_BuildProject}-PERSISTED-STORE`,
      // storage: {
      //   getItem: (key) => ls.get(key),
      //   setItem: (key, value) => ls.set(key, value),
      //   removeItem: (key) => ls.remove(key),
      // },
    }),
  ],
})

export function setupStore(app) {
  app.use(store)
}

export default store
